/**
 * 套保类资产,时间搜索
 */


import React, { PropTypes, PureComponent } from 'react';
import { Form, DatePicker, Button, Row, Col, message } from 'antd';

import moment from 'moment';
import 'moment/locale/zh-cn';
moment.locale('zh-cn');

const FormItem = Form.Item;
const RangePicker = DatePicker.RangePicker;

/**
 * 日期选择控件
 * @param {[type]} dispatch  [方法]
 * @param {[type]} beginDate [开始时间]
 * @param {[type]} endDate   [结束时间]
 * @param {[type]} tag       [所属model]
 */

const dateFormat = 'YYYY/MM/DD';
const nowDate = moment(new Date());
message.config({
  duration: 5,
});
const DateRangePicker=({tag,dispatch,location})=>{
	console.log("location>>>",location)

	const myProps={
		onChange:(date, dateString)=> {
      console.log("vvvvvv",date, dateString);
			if(_.isEmpty(date)){
				message.warning('请选择时间');
			}
      const beginDate = moment(dateString[0]).format('YYYYMMDD');
      const endDate = moment(dateString[1]).format('YYYYMMDD');

			if(beginDate && endDate){
				dispatch({
					type: `${tag}/setState`,
					payload: {
						begindate:beginDate,
						enddate:endDate
					}
				})
			}
		},
	}
	if(location.hash.indexOf("begindate") > -1){
		const arr = location.hash.split('?')[1].split('&')
		const BD = arr[0].split("=")[1];
		const ED = arr[2].split("=")[1];
		return (
			<RangePicker
				{...myProps}
				defaultValue={[moment(BD, dateFormat), moment(ED, dateFormat)]}
				format={dateFormat}
				style={{ width: 200 }}
			/>
		)
	}else{
		return (
			<RangePicker
				{...myProps}
				defaultValue={[moment(nowDate, dateFormat), moment(nowDate, dateFormat)]}
				format={dateFormat}
				style={{ width: 200 }}
			/>
		)
}

}
export default DateRangePicker;
